package cur.nature.admin.dao;

import cur.nature.admin.common.config.DBAdminConfig;
import cur.nature.admin.model.po.Admin_UserPO;
import cur.nature.admin.model.vo.Admin_UserVO;
import cur.nature.framework.core.util.ObjectUtil;
import cur.nature.framework.mvc.auth.LoginUser;
import cur.nature.framework.mvc.crud.CrudDao;
import cur.nature.tinyorm.core.bean.PageBean;

import cur.nature.tinyorm.core.template.DefaultJdbcTemplate;
import cur.nature.framework.core.util.StringUtil;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 用户信息 Dao
 * 作者：Auto Generator By wujian
 * 生成日期：2023-07-10 21:42:14
 */
@Component
@DependsOn(DefaultJdbcTemplate.TEMPLATE)
public class Admin_UserDao extends CrudDao<Admin_UserPO, Admin_UserVO> {
    public Admin_UserDao(@Qualifier(DBAdminConfig.TEMPLATE) DefaultJdbcTemplate template) {
        super(Admin_UserPO.class, Admin_UserVO.class, template);
    }

    public PageBean<Admin_UserVO> list(Admin_UserVO.list_Param param) throws Exception {
        StringBuilder condition = new StringBuilder(" 1=1 ");
        if(StringUtil.isNotEmpty(param.getKeyword())){
            condition.append(" and a.name like '"+param.getKeyword()+"%' ");
        }
        String sql = "select a.name,a.sex,a.state,a.id_type,a.mobile,a.email,a.id_no,a.login_name,a.login_pwd,a.id,a.modify_user,a.modify_date," +
                "(select GROUP_CONCAT(t.name SEPARATOR ',') from  admin_sys_role t, admin_mid_user_role b where t.id=b.role_id and b.user_id=a.id) as deptName," +
                "(select GROUP_CONCAT(t.name SEPARATOR ',') from  admin_sys_dept t, admin_mid_user_dept b where t.id=b.dept_id and b.user_id=a.id) as roleName " +
                " from admin_sys_user a where " + condition;
        PageBean<Admin_UserVO> list = queryForPage(sql, param.buildPage(),
                null, Admin_UserVO.class);
        return list;
    }

    public List<LoginUser.Resource> queryResById(final String userId) throws Exception {
        String sql = "select a.id,a.code,a.parent_id,a.path,a.type,a.name,b.auth " +
                " from admin_sys_resource a, admin_mid_role_resource b, admin_mid_user_role c " +
                " where a.id=b.resource_id and b.role_id=c.role_id and c.user_id="+userId+"";
        List<LoginUser.Resource> list = queryForListCust(sql, LoginUser.Resource.class);
        return list;
    }

    public List<Admin_UserVO.listByKeyword_Res> listByKeyword(Admin_UserVO.listByKeyword_Param param) throws Exception {
        StringBuilder condition = new StringBuilder(" 1=1 ");
        if(StringUtil.isNotEmpty(param.getKeyword())){
            condition.append(" and a.name like '%"+param.getKeyword()+"%' ");
        }
        if(param.getUserIdList() != null && param.getUserIdList().size() > 0){
            condition.append(" and a.id in ("+ StringUtil.convertArrayToStr(param.getUserIdList()) +") ");
        }
        String sql = "select a.id,a.name,a.mobile " +
                " from admin_sys_user a" +
                " where " + condition;
        List<Admin_UserVO.listByKeyword_Res> list = queryForListCust(sql, Admin_UserVO.listByKeyword_Res.class);
        return list;
    }

    public List<Admin_UserVO> queryByCond(Admin_UserVO.login.Param param) throws Exception {
        // 查询出所有的资源
        List<Admin_UserVO> list = queryByCond("and login_name= ? ", new Object[]{param.getUserName()});
        return list;
    }

    public Admin_UserVO queryByOpenId(String openId) throws Exception {
        // 查询出所有的资源
        List<Admin_UserVO> list = queryByCond("and wx_open_id= ? ", new Object[]{openId});
        return ObjectUtil.isEmpty(list) ? null : list.get(0);
    }
}

